home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk63 / simreq / req.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  9KB  |  323 lines

  1. #include <intuition/simplereq.h>
  2.  
  3. /****************************************************************/
  4. /*                                */
  5. /* SimpleRequest(Banner, Gadgets, Window)            */
  6. /*                                */
  7. /*    Banner == string of any length, saying something.    */
  8. /*    Gadgets == a string like "Yes|No", using the vertical    */
  9. /*           bar to separate gadgets (any number).    */
  10. /*    Window == the window (and, essentially, screen) to put    */
  11. /*          the requester in (since it's just a window)   */
  12. /*                                */
  13. /****************************************************************/
  14. /*            Created by Steve Tibbett, 10-March-1989        */
  15. /****************************************************************/
  16. /* Note: It's a first draft.  It's a bit messy, it can be fixed */
  17. /****************************************************************/
  18.  
  19. static struct NewWindow NW;
  20. static struct TextFont *TF;
  21. static struct Window *ReqWindow;        /* The requester's window */
  22. static struct Remember *GadRemember = 0;    /* Intuition Remember struct */
  23. static struct TextFont *RPFont;        /* It's font */
  24. static struct RastPort *RP;            /* Our rastport */
  25.  
  26. SimpleRequest(Banner, GadgetString, Window, Flags)
  27. char *Banner;
  28. char *GadgetString;
  29. struct Window *Window;
  30. short Flags;
  31. {
  32. struct IntuiMessage *Message;        /* For talking to requester */
  33. struct Gadget *IAddress;        /* To check which gadget was hit */
  34. struct Screen *Screen;            /* Screen to appear on */
  35. char buf[255], buf2[255];        /* Buffers... for banner and gads */
  36. char *TextLines[10], *GadLines[10];    /* Pointers into the buffers */
  37. short x;                /* Temporary */
  38. short NumLines = 0, NumGads = 0;    /* Number of text lines & gads */
  39. short LongestLine = 0;            /* Longest text line */
  40. char LongestLineNum = 0;        /* It's number */
  41. short LongestGad = 0;            /* Longest gadget text */
  42. short WindowWidth;            /* Width of the requester window */
  43. short WindowHeight;            /* Height of the requester window */
  44. char YSize;            /* Y size of the source RP's font */
  45. char TopTextY;            /* Where the top line of text will appear */
  46. short temp;            /* Temporary */
  47. short LongestGadLen = 0;    /* Longest gadget's IntuiTextLength() */
  48. char GadgetMode;        /* 0 == horizontal, 1 == vertical */
  49. short ActualGadWidth;        /* Width of ALL gadgets (they're all alike) */
  50. short GadsYStart;        /* Where the gads start on Y */
  51.  
  52. GadgetMode = 0;        /* GadgetMode: 0=Horizontal, 1=Vertical */
  53.  
  54. /* Get the font of the Window that called us */
  55. RPFont = Window->RPort->Font;
  56.  
  57. /* Get the Y Size of that font */
  58. YSize = RPFont->tf_YSize;
  59.  
  60. /* Save a pointer to it's TextFont structure for later */
  61. TF = RPFont;
  62.  
  63. /* First we'll figure how many text lines, and get pointers to each line */
  64.  
  65. strcpy(buf, Banner);
  66. TextLines[NumLines++] = buf;
  67. TextLines[NumLines] = 0;
  68.  
  69. temp = strlen(buf);
  70. for (x=0; x<temp; x++)    
  71.     {
  72.     if (buf[x] == '|')    /* Parse out the '|' */
  73.         {
  74.         TextLines[NumLines++] = &buf[x+1];
  75.         TextLines[NumLines] = 0;
  76.         buf[x] = 0;
  77.         };
  78.     };
  79.  
  80.  
  81. /* Next find the longest line */
  82. for (x=0; x<NumLines; x++)
  83.     {
  84.     char l;
  85.     l = strlen(TextLines[x]);
  86.     if (l > LongestLine) 
  87.         {
  88.         LongestLineNum = x;
  89.         LongestLine = l;    
  90.         };
  91.     };
  92.  
  93. /* Okay, now figure the minimum window width and height */
  94. WindowWidth = 40 + TextLen(TextLines[LongestLineNum]);    /* 20 pixels border on each side */
  95. WindowHeight = (NumLines * (YSize+2)) + 15 + YSize;    /* 3 for the title bar... YSize for the Title Bar. */
  96. if (Flags & SR_NODRAG) WindowHeight -= (YSize+3);
  97. if (Flags & SR_NODRAG)
  98.     TopTextY = YSize+3;
  99.     else TopTextY = YSize+3+YSize+1;
  100.  
  101.  
  102. /** Do the same thing for the gadget line! **/
  103.  
  104. strcpy(buf2, GadgetString);
  105. GadLines[NumGads++] = buf2;
  106. GadLines[NumGads] = 0;
  107. temp = strlen(buf2);
  108. for (x=0; x<temp; x++)
  109.     {
  110.     if (buf2[x] == '|')
  111.         {
  112.         GadLines[NumGads++] = &buf2[x+1];
  113.         GadLines[NumGads] = 0;
  114.         buf2[x] = 0;
  115.         };
  116.     };
  117.  
  118. /* Next find the longest line */
  119.  
  120. for (x=0; x<NumGads; x++)
  121.     {
  122.     short l;
  123.     l = strlen(GadLines[x]);
  124.     if (l > LongestGad) 
  125.         {
  126.         LongestGad = l;    
  127.         };
  128.     
  129.     l = TextLen(GadLines[x]);
  130.     if (l > LongestGadLen) LongestGadLen = l;
  131.     };
  132.  
  133.  
  134. /**********************************************************************/
  135.  
  136. /* Figure the window width and height */
  137. temp = LongestGadLen + TextLen("  ") + 13;    /* 10 offset per gadget */
  138. temp = temp * NumGads;
  139. temp += 48;
  140. if (temp > WindowWidth) WindowWidth = temp;
  141. WindowHeight = WindowHeight + 3 + (2*YSize);
  142.  
  143. /* Go about opening the window */
  144. Screen = Window->WScreen;
  145. GadsYStart = WindowHeight-(YSize+12);
  146.  
  147. if (WindowWidth > Screen->Width)
  148.     {
  149.     GadgetMode = 1;
  150.     WindowHeight = WindowHeight + ((8+YSize)*(NumGads-1));
  151.     WindowWidth = 40 + TextLen(TextLines[LongestLineNum]);    /* 20 pixels border on each side */
  152.     temp = LongestGadLen + TextLen("  ") + 61;
  153.     if (temp > WindowWidth) WindowWidth = temp;
  154.     };
  155.  
  156. if (Flags & SR_NOCENTERWINDOW) {
  157.     NW.LeftEdge = 0;
  158.     NW.TopEdge = 0;
  159.     } else {
  160.     NW.LeftEdge = (Screen->Width/2) - (WindowWidth/2);
  161.     NW.TopEdge = (Screen->Height/2) - (WindowHeight/2);
  162.     };
  163.  
  164. NW.Width = WindowWidth;
  165. NW.Height = WindowHeight;
  166. NW.DetailPen = 0;
  167. NW.BlockPen = 1;
  168. NW.IDCMPFlags = GADGETUP|VANILLAKEY|DISKINSERTED;
  169.  
  170. if (Flags & SR_NODRAG) {
  171.     NW.Flags = ACTIVATE|SMART_REFRESH|NOCAREREFRESH;
  172.     } else NW.Flags = ACTIVATE|WINDOWDRAG|SMART_REFRESH|NOCAREREFRESH;
  173.  
  174. if (Flags & SR_NODRAG) 
  175.     NW.Title = 0;
  176.     else NW.Title = Window->Title;
  177.  
  178. if (Screen->Flags & CUSTOMSCREEN) {
  179.     NW.Type = CUSTOMSCREEN;
  180.     NW.Screen = Screen;
  181.     } else NW.Type = WBENCHSCREEN;
  182.  
  183. /** Open the window **/
  184. ReqWindow = OpenWindow(&NW);
  185. if (ReqWindow == 0) return(FALSE);
  186. RP = ReqWindow->RPort;
  187.  
  188. /** Do the background **/
  189. SetDrMd(RP, JAM2);
  190. SetAPen(RP, 1);
  191. if (Flags & SR_NODRAG)
  192.     RectFill(ReqWindow->RPort, 4, 2, WindowWidth-5, WindowHeight-3);
  193.     else RectFill(ReqWindow->RPort, 4, YSize+3, WindowWidth-5, WindowHeight-3);
  194.  
  195. /** Put the text up **/
  196. SetAPen(RP, 0);
  197. SetBPen(RP, 1);
  198.  
  199. for (x=0; x<NumLines; x++)
  200.     {
  201.     if (Flags & SR_NOCENTERTEXT)
  202.         Move(ReqWindow->RPort, 16, TopTextY + (x*(YSize+1)));
  203.         else Move(ReqWindow->RPort, (WindowWidth/2) - (TextLen(TextLines[x])/2), TopTextY + (x*(YSize+1)));
  204.     Text(ReqWindow->RPort, TextLines[x], strlen(TextLines[x]));
  205.     };
  206.  
  207.  
  208. /** The text is up, now add the gadgets **/
  209. ActualGadWidth = LongestGadLen + TextLen("  ");
  210.  
  211. if (GadgetMode == 0)
  212.     {
  213.     for (x=0; x<NumGads; x++)
  214.         {
  215.         short GadCenter, GadWidth;
  216.          GadWidth = WindowWidth/NumGads;
  217.         GadCenter = (GadWidth * x) + (GadWidth/2);
  218.         CreateGadget(GadCenter-(ActualGadWidth/2)-2, GadsYStart, 
  219.             ActualGadWidth, YSize+3, x, RELVERIFY, GadLines[x]);
  220.         };
  221.     } else
  222.     {
  223.     short GadX;
  224.  
  225.     GadX = WindowWidth/2 - (ActualGadWidth/2) - 2;
  226.  
  227.     for (x=0; x<NumGads; x++)
  228.         {
  229.         CreateGadget(GadX, GadsYStart + ((YSize+8)*x),
  230.             ActualGadWidth, YSize+3, x, RELVERIFY, GadLines[x]);
  231.         };
  232.     };
  233.  
  234.  
  235. /** Okay, it's up, let's wait for something to happen. **/
  236.  
  237. while (TRUE)
  238.     {
  239.     WaitPort(ReqWindow->UserPort);
  240.     Message = (struct IntuiMessage *)GetMsg(ReqWindow->UserPort);
  241.     
  242.     if (Message->Class == GADGETUP)
  243.         {
  244.         IAddress = (struct Gadget *)Message->IAddress;
  245.         
  246.         ReplyMsg((struct Message *)Message);
  247.         CloseWindow(ReqWindow);
  248.         FreeRemember(&GadRemember, TRUE);
  249.  
  250.         return(IAddress->GadgetID);
  251.         };
  252.     ReplyMsg((struct Message *)Message);
  253.     };
  254. }
  255.  
  256.  
  257. /*******************************************************************/
  258. /*  CreateGadget.  Does just that, with your Remember struct.      */
  259. /*******************************************************************/
  260. CreateGadget(GadX, GadY, SizeX, SizeY, GadgetID, Flags, text)
  261. long GadX, GadY, SizeX, SizeY, GadgetID, Flags;
  262. char *text;
  263. {
  264. struct Gadget *Gadget;
  265. short x,y,center;
  266.  
  267. Gadget = (struct Gadget *)AllocRemember((struct Remember **)&GadRemember, sizeof(struct Gadget), MEMF_PUBLIC|MEMF_CLEAR);
  268. if (Gadget == 0) return(FALSE);
  269.  
  270. /* Gadget Imagery.  No text for now.  */
  271. SetAPen(RP, 0);
  272. SetDrMd(RP, JAM2);
  273. RectFill(RP, GadX+5, GadY+3, GadX+4+SizeX, GadY+2+SizeY);
  274. SetAPen(RP, 3);
  275. RectFill(RP, GadX, GadY, GadX+SizeX, GadY+SizeY);
  276. SetAPen(RP, 0);
  277. RectFill(RP, GadX+2, GadY+1, GadX+SizeX-2, GadY+SizeY-1);
  278.  
  279. Gadget->LeftEdge   = GadX+2;
  280. Gadget->TopEdge    = GadY+1;
  281. Gadget->Width      = SizeX-3;
  282. Gadget->Height     = SizeY-1;
  283. Gadget->Flags      = GADGHCOMP;
  284. Gadget->Activation = Flags;
  285. Gadget->GadgetType = BOOLGADGET;
  286. Gadget->GadgetID   = GadgetID;
  287.  
  288. AddGadget(ReqWindow, Gadget, -1);
  289.  
  290. y = GadY + 3 + RPFont->tf_Baseline;
  291. x = TextLen(text);
  292.  
  293. center = GadX + (SizeX/2) - (x/2);
  294.     
  295. SetAPen(RP, 1);
  296. SetBPen(RP, 0);
  297.  
  298. Move(RP, center, y-1);
  299. Text(RP, text, strlen(text));
  300. }
  301.  
  302.  
  303. static struct IntuiText DummyIntuiText = { 0,0, JAM2, 0, 0, 0,0,0 };
  304. static struct TextAttr DummyTA;
  305.  
  306. /**************************************************************/
  307. /* TextLen(text) - Give it a string, it returns the pixel len */
  308. /**************************************************************/
  309. TextLen(text)
  310. char *text;
  311. {
  312. /* set the dummy TextAttr to the TextFont's fields */
  313. DummyTA.ta_Name = TF->tf_Message.mn_Node.ln_Name;
  314. DummyTA.ta_YSize = TF->tf_YSize;
  315.  
  316. /* Set the dummy IntuiText to point to the dummy TextFont and String */
  317. DummyIntuiText.IText = text;
  318. DummyIntuiText.ITextFont = &DummyTA;
  319.  
  320. /* And get the pixel length of the string */
  321. return(IntuiTextLength(&DummyIntuiText));
  322. }
  323.